<?php
    $this->layout('base.phtml', [
        'title' => _('Share collection'),
        'canonical' => url_full('share collection', ['id' => $collection->id]),
        'has_errors' => $error || $errors,
        'modal_enabled' => true,
    ]);
?>

<div class="section">
    <div class="section__title">
        <h1 id="modal-title"><?= _f('Share “%s”', protect($collection->name)) ?></h1>
    </div>

    <p class="section__intro">
        <?php if ($collection->is_public): ?>
            <?= _('This collection is public, anyone can access it.') ?>
        <?php else: ?>
            <?= _('This collection is private, you can share access to other users.') ?>
        <?php endif; ?>
    </p>

    <form method="post" action="<?= url('share collection', ['id' => $collection->id]) ?>">
        <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
        <input type="hidden" name="from" value="<?= $from ?>" />

        <div class="form-group <?= isset($errors['user_id']) ? 'form-group--invalid' : '' ?>">
            <label for="user-id">
                <?= _('Share access with') ?>
            </label>

            <div class="form-group__stack">
                <input
                    id="user-id"
                    name="user_id"
                    type="text"
                    required
                    value="<?= $user_id ?>"
                    aria-describedby="user-id-desc"
                    placeholder="<?= url_full('profile', ['id' => $current_user->id]) ?>"
                />

                <button type="submit" class="button--primary">
                    <?= _('Share') ?>
                </button>
            </div>

            <div id="user-id-desc">
                <?php if (isset($errors['user_id'])): ?>
                    <p class="form-group__error">
                        <span class="sr-only"><?= _('Error') ?></span>
                        <?= $errors['user_id'] ?>
                    </p>
                <?php endif; ?>

                <p class="form-group__caption">
                    <?= _('Copy and paste the <abbr>URL</abbr> of a user’s profile.') ?>
                </p>
            </div>
        </div>

        <div
            class="form-group <?= isset($errors['type']) ? 'form-group--invalid' : '' ?>"
            data-controller="caption-switcher"
        >
            <input
                type="radio"
                id="type-read"
                name="type"
                value="read"
                <?= $type === 'read' ? 'checked' : '' ?>
                aria-describedby="type-desc"
                data-action="caption-switcher#switch"
                data-caption-switcher-target="switch"
            />

            <label class="label--radio" for="type-read">
                <?= _('Read access') ?>
            </label>

            <input
                type="radio"
                id="type-write"
                name="type"
                value="write"
                <?= $type === 'write' ? 'checked' : '' ?>
                aria-describedby="type-desc"
                data-action="caption-switcher#switch"
                data-caption-switcher-target="switch"
            />

            <label class="label--radio" for="type-write">
                <?= _('Read and write access') ?>
            </label>

            <div id="type-desc">
                <?php if (isset($errors['type'])): ?>
                    <p class="form-group__error">
                        <span class="sr-only"><?= _('Error') ?></span>
                        <?= $errors['type'] ?>
                    </p>
                <?php endif; ?>

                <p class="form-group__caption" aria-live="polite">
                    <span data-caption-switcher-target="caption" data-caption-value="read">
                        <?= _('Sharing read access with other users gives them access to the whole collection, <strong>including their hidden links,</strong> but they can’t modify it.') ?>
                    </span>

                    <span data-caption-switcher-target="caption" data-caption-value="write">
                        <?= _('Sharing read and write access with other users gives them access to the whole collection, <strong>including their hidden links,</strong> and they can modify information or add links.') ?>
                    </span>
                </p>
            </div>
        </div>
    </form>

    <?php $collection_shares = $collection->shares(); ?>
    <?php if ($collection_shares): ?>
        <h2 class="title title--small"><?= _('Shared with:') ?></h2>

        <ul class="list list--no-style">
            <?php foreach ($collection_shares as $collection_share): ?>
                <?php $user = $collection_share->user(); ?>
                <li class="list__item line">
                    <span class="line__item">
                        <img class="avatar" src="<?= url_avatar($user->avatar_filename) ?>" alt="" />
                    </span>

                    <span class="line__item line__item--extend">
                        <a class="anchor--hidden" href="<?= url('profile', ['id' => $user->id]) ?>">
                            <?= protect($user->username) ?>
                        </a>
                    </span>

                    <span class="line__item text--secondary">
                        <?php if ($collection_share->type === 'read'): ?>
                            <?= _('read only') ?>
                        <?php else: ?>
                            <?= _('read / write') ?>
                        <?php endif; ?>
                    </span>

                    <form class="line__item" method="post" action="<?= url('delete collection share', ['id' => $collection_share->id]) ?>">
                        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                        <input type="hidden" name="from" value="<?= $from ?>" />

                        <button type="submit" class="button--ghost button--small">
                            <?= icon('times') ?>
                            <span class="no-mobile">
                                <?= _('Revoke') ?>
                            </span>
                        </button>
                    </form>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    <div class="share" data-controller="copy-to-clipboard">
        <span class="share__text" data-copy-to-clipboard-target="copyable"><?= url_full('collection', ['id' => $collection->id]) ?></span>

        <button
            class="share__button"
            data-action="copy-to-clipboard#copy"
            data-copy-to-clipboard-target="feedback"
        >
            <?= icon('copy-to-clipboard') ?>
            <?= _('Copy') ?>
        </button>
    </div>
</div>
